from celery_tasks.sms.yuntongxun.sms import CCP

from celery_tasks.main import celery_app

# sms 存放发送短信的异步相关代码
# 将提供的发送短信的云通讯SDK放到celery_tasks/sms/目录下。

# 建立日志对象
import logging

from verifications.constants import SEND_SMS_TEMP_ID

logger = logging.getLogger("django")


@celery_app.task(name='send_sms_code')
def send_sms_code(mobile, code, expires):
    """
    发送短信验证码
    :param mobile:  手机号
    :param code: 验证码
    :param expires: 过期时间
    :return:
    """

    # 注意： 测试的短信模板编号为1
    try:
        result = CCP.send_template_sms('mobile', [code, expires], SEND_SMS_TEMP_ID)
    except Exception as e:
        logger.error("发送验证码短信[异常][mobile: %s]" % mobile)
    else:
        if result == 0:
            logger.info("发送验证码短信[正常][mobile: %s ]" % mobile)
        else:
            logger.info("发送验证码短信[失败][mobile: %s ]" % mobile)
